package com.naiterui.ehp.bs.cms.modules.agent.controller;

import cn.hutool.core.util.StrUtil;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.exception.BusinessExceptionCode;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.agent.entity.AgAgentArea;
import com.naiterui.ehp.bs.cms.modules.agent.entity.AgArea;
import com.naiterui.ehp.bs.cms.modules.agent.po.AreaPagePO;
import com.naiterui.ehp.bs.cms.modules.agent.po.AreaSavePO;
import com.naiterui.ehp.bs.cms.modules.agent.service.IAgentAreaService;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AreaDetailVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AreaItemVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.IdAndName;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yiren
 * @since 2022/2/10
 */
@Slf4j
@Api(tags = {SwaggerTag.CMS + SwaggerTag.AGENT_MANGEN})
@AllArgsConstructor
@RestController
@RequestMapping("/agent/area/city")
@PreAuthorize("@p.check('agent:area:city')")
public class AgentAreaCityController {

	private final IAgentAreaService agentAreaService;

	@SysLog("查询城市列表")
	@GetMapping("/list")
	@ApiOperation("查询城市列表")
	// @PreAuthorize("@p.check('agent:area:city:list')")
	public PageVO<AreaItemVO> list(PageParamsVO pageParamsVO, AreaPagePO pagePO) {
		return agentAreaService.queryPage(pageParamsVO, pagePO, AgAgentArea.AREA_LEV_PROVINCE);
	}
	@SysLog("查询城市详情")
	@GetMapping("/get")
	@ApiOperation("查询城市详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "城市ID", dataType = "long", paramType = "query", required = true),
	})
	// @PreAuthorize("@p.check('agent:area:city:get')")
	public AreaDetailVO get(Long id) {
		return agentAreaService.getDetail(id);
	}

	@SysLog("修改城市数据")
	@PostMapping("/save")
	@ApiOperation("修改城市数据")
	// @PreAuthorize("@p.check('agent:area:city:save')")
	public void save(@RequestBody AreaSavePO savePO) throws BusinessException {
		if (savePO.getId() == null || StrUtil.hasEmpty(savePO.getAreaName())){
			throw new BusinessException(BusinessExceptionCode.PARAM_ERROR);
		}
		if (savePO.getAreaName().length() > 40){
			throw new BusinessException(BusinessExceptionCode.PARAM_ERROR, "省区名称最大长度为40");
		}
		agentAreaService.saveArea(savePO, AgArea.AREA_LEV_PROVINCE);
	}
	@SysLog("查询大区可更改城市(下拉框使用)")
	@GetMapping("/find")
	@ApiOperation("查询大区可更改城市(下拉框使用)")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaId", value = "大区ID", dataType = "long", paramType = "query", required = true),
	})
	// @PreAuthorize("@p.check('agent:area:city:find')")
	public List<IdAndName> find(Long areaId) {
		return agentAreaService.findCanChangeCity(areaId);
	}

	@SysLog("查询所有城市(下拉框使用)")
	@GetMapping("/findAll")
	@ApiOperation("查询所有城市(下拉框使用)")
	public List<IdAndName> findAll() {
		return agentAreaService.findCityAll();
	}
}
